package com.chen.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.domain.EntrustOrder;
import com.chen.dto.R;
import com.chen.model.EntrustOrderParam;
import com.chen.service.EntrustOrderService;
import com.chen.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: chen
 * @Date: 2021/5/18 2:14 PM
 * @Version 1.0
 */
@RestController
@Api(tags = "交易委托控制器")
@RequestMapping("/entrustOrders")
public class EntrustOrdersController {

    @Autowired
    private EntrustOrderService entrustOrderService;

    @GetMapping
    public R<Page<EntrustOrder>> list(Page<EntrustOrder> page, Integer type, String symbol) {
        Page<EntrustOrder> list = entrustOrderService.page(page, new LambdaQueryWrapper<EntrustOrder>()
                .eq(EntrustOrder::getUserId, UserUtil.getUser())
                .eq(type != null, EntrustOrder::getType, type)
                .eq(StringUtils.isNotBlank(symbol), EntrustOrder::getSymbol, symbol)
                .orderByDesc(EntrustOrder::getCreated));
        return R.ok(list);
    }

    @ApiOperation("新增买入的委托单")
    @PostMapping
    public R buy(@RequestBody EntrustOrderParam entrustOrderParam) {
        entrustOrderService.buy(entrustOrderParam);
        return R.ok();
    }

    @GetMapping("/{symbol}")
    public R<Page<EntrustOrder>> list(Page<EntrustOrder> page,
                                      @PathVariable String symbol) {

        Page<EntrustOrder> list = entrustOrderService.page(page, new LambdaQueryWrapper<EntrustOrder>()
                .eq(EntrustOrder::getUserId, UserUtil.getUser())
                .eq(EntrustOrder::getSymbol, symbol)
                .eq(EntrustOrder::getStatus, 0)
                .orderByDesc(EntrustOrder::getCreated)
        );
        return R.ok(list);
    }

    @GetMapping("/history/{symbol}")
    public R<Page<EntrustOrder>> historyList(Page<EntrustOrder> page,
                                             @PathVariable String symbol) {

        Page<EntrustOrder> list = entrustOrderService.page(page, new LambdaQueryWrapper<EntrustOrder>()
                .eq(EntrustOrder::getUserId, UserUtil.getUser())
                .eq(EntrustOrder::getSymbol, symbol)
                .in(EntrustOrder::getStatus, 1, 2)
                .orderByDesc(EntrustOrder::getCreated)
        );
        return R.ok(list);
    }


}
